package server;

import server.util.Context;
import server.util.Host;
import server.util.Mapper;
import server.util.Wrapper;

import java.io.InputStream;
import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

    private Socket socket;
    private Mapper mapper;
    /*private Map<String,HttpServlet> servletMap;

    public RequestProcessor(Socket socket, Map<String, HttpServlet> servletMap) {
        this.socket = socket;
        this.servletMap = servletMap;
    }*/

    public RequestProcessor(Socket socket, Mapper mapper) {
        this.socket = socket;
        this.mapper = mapper;
    }

    @Override
    public void run() {
        try{
            InputStream inputStream = socket.getInputStream();

            // 封装Request对象和Response对象
            Request request = new Request(inputStream);
            Response response = new Response(socket.getOutputStream());

            /*// 静态资源处理
            if(servletMap.get(request.getUrl()) == null) {
                response.outputHtml(request.getUrl());
            }else{
                // 动态资源servlet请求
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request,response);
            }*/

            Servlet servlet = null;

            // 查找对应的Host
            Host mappedHost = mapper.getMappedHost().get(request.getHost());
            // 查找对应的Context
            Map<String, Context> contextList = mappedHost.getMappedContext();
            Context mappedContext = null;
            for (Map.Entry<String, Context> mappedContextEntry : contextList.entrySet()) {
                if(request.getRequestUrl().startsWith(mappedContextEntry.getKey())){
                    mappedContext = mappedContextEntry.getValue();
                }
            }
/*
            // 如果是静态资源
            if(servletMap.get(request.getUrl()) == null){
                response.outputHtml(request.getUrl());
            } else{
                HttpServlet httpServlet = servletMap.get(request.getUrl());
                httpServlet.service(request,response);
            }*/

            Map<String, Wrapper> mappedWrapperMap = null;
            Servlet httpServlet = null;
            if(mappedContext == null){
                throw new Exception("没有定位到对应的web应用程序");
            }else{
                mappedWrapperMap = mappedContext.getMappedWrapper();
            }
            for (Map.Entry<String, Wrapper> wrapperEntry : mappedWrapperMap.entrySet()) {
                if(wrapperEntry.getKey().equalsIgnoreCase(request.getRequestUrl())){
                    httpServlet = wrapperEntry.getValue().getServlet();
                    break;
                }
            }
            // 如果是静态资源
            if(httpServlet == null){
                response.outputHtml(mappedContext,request);
            }else{
                httpServlet.service(request,response);
            }

            socket.close();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
